#!/bin/bash
################################################################################
# File Name: auto_init_cluster.sh
# Version: V1.0
# Author: Brian Hsiung
# Created Time : 2021-05-16 20:17:23
# Description:  初始化集群
###############################################################################
source /etc/profile

# 此处端口需要与auto_install_mongodb.sh定义的保持一致
MONGOS_PORT=20000
CONFIG_SERVER_PORT=21000
SHARD_SERVER1_PORT=27001
SHARD_SERVER2_PORT=27002
SHARD_SERVER3_PORT=27003

# 密码文件路径，需要根据实际情况修改，注意不要放在/root目录
PASSWD_PATH=/opt/shell-mongodb-cluster/passwd.js

# 默认3台主机IP根据实际情况修改，需要与auto_install_mongodb.sh保持一致
IPS=(192.168.157.131 192.168.157.132 192.168.157.133)

MONGODB_CLUSTER_HOME=/opt/mongodb_cluster
CONF_HOME=$MONGODB_CLUSTER_HOME/conf

function wait_time() {
    echo -e "==================== 等待10s ====================\n"
    for i in `seq 10`
    do
        echo -n '...'
        sleep 1
    done
    echo ""
}

function init_config_server() {
    # rs.status()
    echo -e "==================== 初始化config server集群 ====================\n"
su - mongodb << EOF
mongo --port $CONFIG_SERVER_PORT --eval ' 
rs.initiate(
    {
        _id: "configs",
        configsvr: true,
        members: [
            { _id : 0, host : "${IPS[0]}:$CONFIG_SERVER_PORT"},
            { _id : 1, host : "${IPS[1]}:$CONFIG_SERVER_PORT"},
            { _id : 2, host : "${IPS[2]}:$CONFIG_SERVER_PORT"}
        ]
    }
)'
EOF
}

function init_shard_servers() {
    ID="$1"
    PORT="$2"
    wait_time
    #rs.status()
    echo -e "==================== 初始化 $ID 集群 ====================\n"
su - mongodb <<EOF 
mongo --port $PORT --eval '
rs.initiate(
  {
    _id: "$ID",
    members: [
      { _id : 0, host : "${IPS[0]}:$PORT" },
      { _id : 1, host : "${IPS[1]}:$PORT" },
      { _id : 2, host : "${IPS[2]}:$PORT" }
    ]
  }
)'
EOF
}

function init_mongos() {
    wait_time # 需等个几秒钟，否则可能会报错
    echo -e "==================== 启用分片集群 ====================\n"
su - mongodb << EOF
mongo --port $MONGOS_PORT --eval '
sh.addShard("shard_server1/${IPS[0]}:$SHARD_SERVER1_PORT,${IPS[1]}:$SHARD_SERVER1_PORT,${IPS[2]}:$SHARD_SERVER1_PORT")
sh.addShard("shard_server2/${IPS[0]}:$SHARD_SERVER2_PORT,${IPS[1]}:$SHARD_SERVER2_PORT,${IPS[2]}:$SHARD_SERVER2_PORT")
sh.addShard("shard_server3/${IPS[0]}:$SHARD_SERVER3_PORT,${IPS[1]}:$SHARD_SERVER3_PORT,${IPS[2]}:$SHARD_SERVER3_PORT")
sh.status()'
EOF
}

function init_users() {
    wait_time # 等待10s
    echo -e "==================== 创建管理员用户 ====================\n"
    su - mongodb -c "mongo --nodb $PASSWD_PATH"
}

function passwd_auth() {
    wait_time # 等待10s
    echo -e "==================== 本机启用访问控制 ====================\n"
    chmod 0400 $MONGODB_CLUSTER_HOME/mongo.key
    chown mongodb:mongodb -R $MONGODB_CLUSTER_HOME/mongo.key
tee -a $CONF_HOME/mongos.conf <<-EOF
security:
  keyFile: $MONGODB_CLUSTER_HOME/mongo.key
EOF
for conf in config shard_server1 shard_server2 shard_server3
do
tee -a $CONF_HOME/$conf.conf <<-EOF
security:
  authorization: enabled
  keyFile: $MONGODB_CLUSTER_HOME/mongo.key
EOF
done
}

function sshcopy() {
    IP="$1"
    echo -e "==================== $IP启用访问控制 ====================\n"
    scp $MONGODB_CLUSTER_HOME/mongo.key root@$IP:$MONGODB_CLUSTER_HOME
    scp $MONGODB_CLUSTER_HOME/conf/* root@$IP:$MONGODB_CLUSTER_HOME/conf
    ssh root@$IP << EOF
chmod 0400 $MONGODB_CLUSTER_HOME/mongo.key
chown mongodb:mongodb -R $MONGODB_CLUSTER_HOME/conf
chown mongodb:mongodb -R $MONGODB_CLUSTER_HOME/mongo.key
EOF
}

echo -e "==================== MongoDB Cluster初始化 ====================\n"

DIR=$(cd $(dirname $0) && pwd)
cd $DIR

init_config_server
init_shard_servers shard_server1 $SHARD_SERVER1_PORT
init_shard_servers shard_server2 $SHARD_SERVER2_PORT
init_shard_servers shard_server3 $SHARD_SERVER3_PORT
init_mongos
init_users
openssl rand -base64 756 > $MONGODB_CLUSTER_HOME/mongo.key
passwd_auth
sshcopy ${IPS[1]}
sshcopy ${IPS[2]}
echo -e "==================== 每个服务器执行service mongodb restart ====================\n"
